package com.jeeplus.test.tools.resp;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import org.modelmapper.Conditions;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

import java.util.Map;

public class Convert2Bean{
    private void BeanConvert() {
    }

    public static <T> T convertBean(Object source, Class<T> clz) {
        return ObjectUtil.isNotEmpty(source) ? getModelMapper().map(source, clz) : null;
    }

    public static <T> T convertMap2Bean(Map<String, Object> source, Class<T> clz) {
        return BeanUtil.mapToBean(source, clz, true, CopyOptions.create());
    }

    public static Map<String, Object> convertBean2Map(Object source) {

        return BeanUtil.beanToMap(source, false, true);
    }

    private static ModelMapper getModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        modelMapper.getConfiguration().setFieldMatchingEnabled(true);
        modelMapper.getConfiguration().setSkipNullEnabled(true);
        modelMapper.getConfiguration().setAmbiguityIgnored(true);
        return modelMapper;
    }



}
